<template>
  <div>
    <el-row style="margin: 10px">
      <el-col :span="4">
        <el-input v-model="searchPhone" placeholder="通过房东手机号搜索" clearable maxlength="11" />
      </el-col>
      <el-col :span="4" style="margin-left: 20px">
        <el-input v-model="searchName" placeholder="通过房东用户名搜索" clearable maxlength="15" />
      </el-col>
      <el-col :span="4" style="margin-left: 20px">
        <el-input v-model="searchRealName" placeholder="通过房东真实姓名搜索" clearable maxlength="5" />
      </el-col>
      <el-col :span="3" style="margin-left: 20px">
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </el-col>
    </el-row>
    <el-table :data="userData" style="width: 100%">
      <el-table-column label="标题" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" style="width: 300px" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.district + scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="介绍" align="center">
        <template slot-scope="scope">
          <span v-if="!scope.row.introduce">无</span>
          <el-popover v-else trigger="hover" placement="top">
            <p>介绍: {{ scope.row.introduce }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.introduce }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="房间格局" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.room }}房{{ scope.row.parlor }}厅{{ scope.row.lavatory }}卫</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>用户名: {{ scope.row.username }}</p>
            <p>真实姓名: {{ scope.row.realName || '未填写' }}</p>
            <p>联系电话: {{ scope.row.phone }}</p>
            <p>电子邮箱: {{ scope.row.email || '未填写' }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.username }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" width="240" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px"><el-date-picker type="datetime" :value="scope.row.createTime" readonly /></span>
        </template>
      </el-table-column>
      <el-table-column label="是否删除" width="180" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.isDelete === true" style="color: #F6416C">已删除</span>
          <span v-else>未删除</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!--          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" type="text">编辑</el-button>-->
          <el-button v-if="scope.row.isBan === false" size="mini" type="text" style="color: #F6416C" @click="ban(scope.$index, scope.row)">下架</el-button>
          <el-button v-else-if="scope.row.isBan === true" size="mini" type="text" @click="cancelBan(scope.$index, scope.row)">上架</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="12" :offset="14" style="margin-top: 20px">
      <el-pagination
        background
        layout="prev, pager, next, jumper, sizes"
        :current-page="page.currentPage"
        :total="page.total"
        :pager-count="5"
        :page-size="page.pageSize"
        :page-sizes="page.pageSizes"
        @current-change="pageChange"
        @size-change="sizeChange"
      />
    </el-col>
  </div>
</template>

<script>

import { getHouseList, ban, cancelBan } from '@/api/houseManage'

export default {
  filters: {},
  data() {
    return {
      userData: [],
      searchPhone: '',
      searchName: '',
      searchRealName: '',
      page: {
        pageSize: 5,
        pageSizes: [5, 10, 20, 40, 80],
        totalPage: 1,
        total: 0,
        currentPage: 1
      }
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getHouseList({
        page: this.page.currentPage,
        limit: this.page.pageSize,
        searchPhone: this.searchPhone,
        searchName: this.searchName,
        searchRealName: this.searchRealName
      }).then((res) => {
        if (JSON.parse(res.data.list).length <= 0) {
          this.$message({
            message: '获取不到房源信息~',
            type: 'warning'
          })
        }
        this.userData = JSON.parse(res.data.list)
        this.page.total = res.data.total
        this.page.totalPage = res.data.totalPage
      })
    },
    handleSearch() {
      this.page.currentPage = 1
      this.getList()
    },
    handleEdit(index, row) {
      console.log(index, row)
    },
    ban(index, row) {
      ban(row).then((res) => {
        if (res.code === 200) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getList()
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      })
    },
    cancelBan(index, row) {
      cancelBan(row).then((res) => {
        if (res.code === 200) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getList()
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      })
    },
    pageChange(page) {
      this.page.currentPage = page
      this.getList()
    },
    sizeChange(size) {
      this.page.pageSize = size
      this.getList()
    }
  }
}
</script>
